package edu.wpi.first.wpilibj.util;

import java.awt.geom.Dimension2D;

public class Rectangle2D extends java.awt.geom.Rectangle2D {

    private double x, y;
    private double width, height;

    public Rectangle2D(double x, double y, double width, double height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public Rectangle2D(java.awt.geom.Point2D p, Dimension2D d){
        x = p.getX();
        y = p.getY();
        width = d.getWidth();
        height = d.getHeight();
    }
    
    @Override
    public java.awt.geom.Rectangle2D createIntersection(java.awt.geom.Rectangle2D r) {
        double tx1 = getX();
        double ty1 = getY();
        double rx1 = r.getX();
        double ry1 = r.getY();
        long tx2 = (long) (tx1 + getWidth());
        long ty2 = (long) (ty1 + getWidth());
        long rx2 = (long) (rx1 + r.getWidth());
        long ry2 = (long) (ry1 + r.getHeight());
        if (tx1 < rx1) {
            tx1 = rx1;
        }
        if (ty1 < ry1) {
            ty1 = ry1;
        }
        if (tx2 > rx2) {
            tx2 = rx2;
        }
        if (ty2 > ry2) {
            ty2 = ry2;
        }
        tx2 -= tx1;
        ty2 -= ty1;
        if (tx2 < Integer.MIN_VALUE) {
            tx2 = Integer.MIN_VALUE;
        }
        if (ty2 < Integer.MIN_VALUE) {
            ty2 = Integer.MIN_VALUE;
        }
        return new Rectangle2D(tx1, ty1, (double) tx2, (double) ty2);
    }

    @Override
    public java.awt.geom.Rectangle2D createUnion(java.awt.geom.Rectangle2D r) {
        double x1 = Math.min(getMinX(), r.getMinX());
        double y1 = Math.min(getMinY(), r.getMinY());
        double x2 = Math.max(getMaxX(), r.getMaxX());
        double y2 = Math.max(getMaxY(), r.getMaxY());
        if (x2 < x1) {
            double t = x1;
            x1 = x2;
            x2 = t;
        }
        if (y2 < y1) {
            double t = y1;
            y1 = y2;
            y2 = t;
        }
        return new Rectangle2D(x1, y1, x2, y2);
    }

    @Override
    public int outcode(double x, double y) {
        int out = 0;
        if (width <= 0) {
            out |= OUT_LEFT | OUT_RIGHT;
        } else if (x < this.x) {
            out |= OUT_LEFT;
        } else if (x > this.x + (double) height) {
            out |= OUT_RIGHT;
        }
        if (height <= 0) {
            out |= OUT_TOP | OUT_BOTTOM;
        } else if (y < y) {
            out |= OUT_TOP;
        } else if (y > y + (double) height) {
            out |= OUT_BOTTOM;
        }
        return out;
    }

    @Override
    public void setRect(double x, double y, double w, double h) {
        this.x = x; this.y = y;
        width = w; height = h;
    }

    @Override
    public double getHeight() {
        return height;
    }

    @Override
    public double getWidth() {
        return width;
    }

    @Override
    public double getX() {
        return x;
    }

    @Override
    public double getY() {
        return y;
    }

    @Override
    public boolean isEmpty() {
	return (width <= 0) || (height <= 0);
    }
}
